import tarfile
from pathlib import Path

path = 'test'
# 创建压缩包
tar = tarfile.open('new.tar.gz', 'w:gz')

for p in Path(path).rglob('*'):
    tar.add(p)

tar.close()
print('done!')